package com.itany.book.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.exception.RequestParamsException;
import com.itany.mapper.PermissionMapper;
import com.itany.pojo.Permission;
import com.itany.service.PermissionService;
import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public PageInfo<Permission> findPermissionAll(Integer page, Integer rows,String parentnameKey, String premissionKey)  {
        PageHelper.startPage(page, rows);
        List<Permission> list;
        if(!StrUtil.isBlankIfStr(parentnameKey)){
            for(int i =1;i<=Constant.ROOT_PERMISSION_STR.length();i++){
                if(Constant.ROOT_PERMISSION_STR.substring(0,i).equals(parentnameKey)){
                    parentnameKey = "root";
                    break;
                }
            }
        }
        list = permissionMapper.selectAll(StringUtils.escapeStr(parentnameKey), StringUtils.escapeStr(premissionKey));
        return new PageInfo<>(list);
    }

    /**
     * 查找所有根权限
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Permission> findAllRootPermissions() {
        return permissionMapper.selectByParent(Constant.ROOT_PERMISSION);
    }


    /**
     * 根据父权限查找权限
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Permission> findPermissionsByParent(Integer parentid) {
        return permissionMapper.selectByParent(parentid);
    }

    /**
     * 更新部分权限
     *
     * @param id       权限ID
     * @param name     权限名称
     * @param sname    权限操作
     * @param parentid 父权限id
     */
    @Override
    public void modifyPermission(String id, String name, String sname, String parentid) throws RequestParamsException {
        if (StrUtil.isEmptyIfStr(id)) {
            throw new RequestParamsException("请求参数缺失");
        }
        Integer idInt;
        Integer parentidInt = null;
        try {
            idInt = Integer.parseInt(id);
            if (!StrUtil.isEmptyIfStr(parentid)) {
                parentidInt = Integer.parseInt(parentid);
            }
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
        Permission permission = new Permission();
        permission.setId(idInt);
        permission.setName(name);
        permission.setSname(sname);
        Permission parentPermission = new Permission();
        parentPermission.setId(parentidInt);
        permission.setParentPermission(parentPermission);
        permissionMapper.updatePermission(permission);
    }
}
